package com.dawn.module.hospital.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.http.HttpUtils;
import com.dawn.module.hospital.controller.app.base.vo.EReceiptOpenReqVO;
import com.dawn.module.hospital.enums.BHConstants;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:电子发票
 * @author: cnsu-cmh
 * @time: 2024/8/8 18:06
 */
@Slf4j
@Service
public class EInvoiceService extends BHService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private PatientService patientService;

    @Autowired
    private EhealthService ehealthService;

    public CommonResult open(String orderType,Map map) {
        EReceiptOpenReqVO reqVO = new EReceiptOpenReqVO();
        if(BHConstants.ORDER_TYPE_REG.equals(orderType)){
            reqVO.setOrderNumber(String.valueOf(map.get("registerNo")));
            reqVO.setTotalCost(String.valueOf(map.get("amount")));
            reqVO.setOperateType("4");
            reqVO.setSettleId(String.valueOf(map.get("procedureId")));
        } else if (BHConstants.ORDER_TYPE_RECIPE.equals(orderType)) {
            reqVO.setOrderNumber(String.valueOf(map.get("orderNumber")));
            reqVO.setTotalCost(String.valueOf(map.get("fee")));
            reqVO.setOperateType("1");
            reqVO.setSettleId(String.valueOf(map.get("settleId")));
        } else if (BHConstants.ORDER_TYPE_PRE_PAYMENT.equals(orderType)) {
            reqVO.setOrderNumber(String.valueOf(map.get("orderNumber")));
            reqVO.setTotalCost(String.valueOf(map.get("payAmount")));
            reqVO.setOperateType("2");
            reqVO.setSettleId(String.valueOf(map.get("settleId")));
        }

        int amount = new BigDecimal(reqVO.getTotalCost()).multiply(new BigDecimal(100)).intValue();

        if(amount <= 0){
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(),"订单金额0，无需开票");
        }

        reqVO.setPatientId(String.valueOf(map.get("patientId")));
        reqVO.setPatientName(String.valueOf(map.get("patientName")));
        reqVO.setUserId(String.valueOf(map.get("userId")));
        Map args = new HashMap<String,Object>();
        args.put("hisProvider",BHConstants.HIS_PROVIDER);
        args.put("tradeSerialNumber", RandomUtil.randomNumbers(9));
        args.put("operator",getEInvoiceOperator());
        args.put("baseUrl",getEInvoiceOpenUri());
        args.put("orderNumber",reqVO.getOrderNumber());
        args.put("operateType",reqVO.getOperateType());
        args.put("settleId",reqVO.getSettleId());
        args.put("totalCost",reqVO.getTotalCost());
        args.put("patientName",reqVO.getPatientName());
        args.put("patientId",reqVO.getPatientId());
        args.put("userId",reqVO.getUserId());
        CommonResult httpResult = HttpUtils.getHttp(getHisServerUri() + BHConstants.HIS_E_RECEIPT_OPEN.getValue(), args);
        return httpResult;
    }

    public CommonResult cancel(String orderNumber) {
        Map args = new HashMap<String,Object>();
        args.put("hisProvider",BHConstants.HIS_PROVIDER);
        args.put("orderNumber",orderNumber);
        args.put("baseUrl",getEInvoiceCancelUri());
        CommonResult httpResult = HttpUtils.getHttp(getHisServerUri() + BHConstants.HIS_E_RECEIPT_CANCEL.getValue(), args);
        return httpResult;
    }

    public CommonResult query(String orderNumber) {
        Map args = new HashMap<String,Object>();
        args.put("hisProvider",BHConstants.HIS_PROVIDER);
        args.put("orderNumber",orderNumber);
        CommonResult httpResult = HttpUtils.getHttp(getHisServerUri() + BHConstants.HIS_E_RECEIPT_QUERY.getValue(), args);
        return httpResult;
    }

}
